package jjn.round1;

import java.util.List;
import java.util.Objects;

/**
 * @author Jiang Jining
 * @since 2023-01-16 23:19
 */
public class LeetCode1813_SentenceSimilarity3 {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        String[] words1 = sentence1.split(" ");
        String[] words2 = sentence2.split(" ");
        List<String> fewer;
        List<String> more;
        if (words2.length > words1.length) {
            more = List.of(words2);
            fewer = List.of(words1);
        } else {
            fewer = List.of(words2);
            more = List.of(words1);
        }
        // 匹配前缀
        int front = 0, back = 0;
        int min = fewer.size();
        int i = 0;
        for (; i < min; i++) {
            if (Objects.equals(fewer.get(i), more.get(i))) {
                front++;
            } else {
                break;
            }
        }
        for (int j = 0; j < min - i; j++) {
            if (Objects.equals(fewer.get(fewer.size() - 1 - j), more.get(more.size() - 1 - j))) {
                back++;
            } else {
                break;
            }
        }
        return (front + back) == min;
    }
}
